package xyz.quanguanjia.masterboy;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import androidx.recyclerview.widget.DividerItemDecoration;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.google.gson.Gson;
import com.google.gson.internal.LinkedTreeMap;

import java.util.ArrayList;
import java.util.HashMap;

import xyz.quanguanjia.masterboy.adapter.GroupDetailAdapter;
import xyz.quanguanjia.masterboy.adapter.MBDGroupCommentsModel;
import xyz.quanguanjia.masterboy.decoration.GroupDetailItemDecoration;

public class MBDPostDetailActivity extends AppCompatActivity implements GroupDetailItemDecoration.GroupDetailItemClickListener {

    private RecyclerView recyclerView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_post_detail);

        //导航栏
        Toolbar toolbar = findViewById(R.id.toolbar);
        MBDToolbarUtils.setCenterTitle(toolbar, "详情");
        MBDToolbarUtils.backButtonClicked(toolbar, new Runnable() {
            @Override
            public void run() {
                finish();
            }
        });
//        MBDToolbarUtils.toolButtonClicked(toolbar, R.id.img_toolbar_more, new View.OnClickListener() {
//            @Override
//            public void onClick(View view) {
//                MBDGroupDetailActivity.this.showActionSheet();
//            }
//        });

        this.recyclerView = findViewById(R.id.recyclerView_group_detail);

        // 设置上拉加载更多监听器
        recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrolled(@NonNull RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);
                // 在这里判断是否到达列表底部，触发加载更多的操作
            }
        });

        LinearLayoutManager layoutManager = new LinearLayoutManager(MBDApplication.getContext());
        recyclerView.setLayoutManager(layoutManager);
        GroupDetailItemDecoration hi = new GroupDetailItemDecoration(this);
        recyclerView.addItemDecoration(hi);
        recyclerView.addItemDecoration(new DividerItemDecoration(MBDApplication.getContext(), LinearLayoutManager.VERTICAL));

        this.refreshData();
    }

    public void refreshData(){

        // 在接收端的 Activity 中获取传递的实体对象
        Intent intent = getIntent();
        MBDGroupDetailModel detail = (MBDGroupDetailModel) intent.getSerializableExtra("detail");
        GroupDetailAdapter adapter = new GroupDetailAdapter(detail);
        MBDPostDetailActivity.this.recyclerView.setAdapter(adapter);

        this.doGroupDetailRequest(detail.groupId,new MBDPostDetailActivity.CallBack() {
            @Override
            public void completion(MBDGroupDetailModel detail) {
                if (detail != null) {
                    // 处理成功的响应
                    adapter.setGroupDetail(detail);
                    adapter.notifyDataSetChanged();

                }
            }
        });
    }

    //快速排序

    private void doGroupDetailRequest(String groupId, MBDPostDetailActivity.CallBack callBack) {
        assert !TextUtils.isEmpty(groupId);

        HashMap<String,String > params = new HashMap<String,String>();
        params.put("groupId",groupId);
        MBDHttpClient.shared().doRequest(ServiceType.SERVICE_GROUP_GET_INFO, params, new MBDHttpClient.Callback() {
            @Override
            public void completion(boolean success, Error error, MBDModelResponse response) {
                if (success){
                    LinkedTreeMap<String,Object> linkedTreeMap = (LinkedTreeMap<String, Object>) response.data;
                    Gson gson = new Gson();
                    String jsonString = gson.toJson(linkedTreeMap);
                    MBDGroupDetailModel rst= gson.fromJson(jsonString, MBDGroupDetailModel.class);
                    callBack.completion(rst);
                } else {
                    Toast.makeText(MBDPostDetailActivity.this, error.getMessage(), Toast.LENGTH_SHORT).show();
                }

            }
        });

    }

    public interface CallBack {
        public void completion(MBDGroupDetailModel detail);
    }

    public interface CommentsRequestCallBack {
        public void completion(ArrayList<MBDGroupCommentsModel> commentsModel);
    }

    @Override
    protected void onStart() {
        super.onStart();
        Log.d("Lifecycle", "onStart");
    }

    @Override
    protected void onResume() {
        super.onResume();
        Log.d("Lifecycle", "onResume");
    }

    @Override
    protected void onPause() {
        super.onPause();
        Log.d("Lifecycle", "onPause");
    }

    @Override
    protected void onStop() {
        super.onStop();
        Log.d("Lifecycle", "onStop");
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        Log.d("Lifecycle", "onDestroy");
    }

    @Override
    protected void onRestart() {
        super.onRestart();
        Log.d("Lifecycle", "onRestart");
    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        Log.d("Lifecycle", "onSaveInstanceState");
    }

    @Override
    protected void onRestoreInstanceState(Bundle savedInstanceState) {
        super.onRestoreInstanceState(savedInstanceState);
        Log.d("Lifecycle", "onRestoreInstanceState");
    }

    @Override
    public void onItemClick(int position) {

    }
}
